/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.oet.webservice.basic;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.cxf.transport.http.HTTPSession;

import org.apache.cxf.binding.soap.SoapFault;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.SoapVersion;
import org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.Session;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.ws.security.WSSecurityException;

//import com.ydk.account.persistence.entity.YdkAccount;

public class SoapSessionInterceptor extends AbstractPhaseInterceptor<Message> {

	private static final Logger LOG = LogUtils.getL7dLogger(SoapSessionInterceptor.class);
    public SoapSessionInterceptor() {
        super(Phase.RECEIVE);
//        addBefore(SoapPreProtocolInInterceptor.class.getName());
    }
    
    public void handleMessage(Message message) {
        //TODO

    	boolean isInbound = false;

        Exchange ex = message.getExchange();
        isInbound = message == ex.getInMessage() 
        	|| message == message.getExchange().getInFaultMessage();

        if (isInbound) {
	        Session s = ex.getSession();
	        HTTPSession session = (HTTPSession)s;
	        
	        if (session != null)
	        {
//	        	YdkAccount userSession = (YdkAccount)session.get("userLogIn");
		        
		        if (false)
//	        	if (userSession == null	)
	        	{
		        	
		        	try {
						throw new Exception("Please log in first");
					} catch (Exception e) {
						// TODO Auto-generated catch block
//						e.printStackTrace();
//						LOG.log(Level.WARNING, "", e);
			            SoapFault fault = new SoapFault(
			            		new org.apache.cxf.common.i18n.Message("Need login", LOG), 
			            		e, ((SoapMessage)message).getVersion().getSender());
			            throw fault;
					}
		        }
	        }
        }
//        Object user = session.getAttribute("")
        
        
        
    }

    public void handleFault(SoapMessage message) {
    }
    
}
